﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>人员信息编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../static/css/font.css">
    <link rel="stylesheet" href="../../../static/css/weadmin.css">
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
    <div class="weadmin-body">
        <form class="layui-form">
            <div class="layui-form-item">
                <label for="EmpID" class="layui-form-label">
                    <span class="we-red">*</span>工号
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="EmpID" name="EmpID" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
                <!--<div class="layui-form-mid layui-word-aux">
            请设置至少5个字符，将会成为您唯一的登录名
        </div>-->
                <label for="EmpName" class="layui-form-label">
                    <span class="we-red">*</span>姓名
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="EmpName" name="EmpName" lay-verify="required|nikename" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="EmpPwd" class="layui-form-label">
                    <span class="we-red">*</span>密码
                </label>
                <div class="layui-input-inline">
                    <input type="password" id="EmpPwd" name="EmpPwd" lay-verify="required|pass" autocomplete="off" class="layui-input">
                </div>
                <!--<div class="layui-form-mid layui-word-aux">
            6到16个字符
        </div>-->
                <label for="ReEmpPwd" class="layui-form-label">
                    <span class="we-red">*</span>确认密码
                </label>
                <div class="layui-input-inline">
                    <input type="password" id="ReEmpPwd" name="ReEmpPwd" lay-verify="required|repass" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="EmpAge" class="layui-form-label">
                    <span class="we-red">*</span>年龄
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="EmpAge" name="EmpAge" lay-verify="required|nikename" autocomplete="off" class="layui-input">
                </div>
                <label for="EmpSex" class="layui-form-label">性别</label>
                <div class="layui-input-inline" id="EmpSex">
                    <input type="radio" name="EmpSex" value="男" title="男" checked>
                    <input type="radio" name="EmpSex" value="女" title="女">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="EmpNation" class="layui-form-label">
                    <span class="we-red">*</span>民族
                </label>
                <div class="layui-input-inline">
                    <select id="EmpNation" name="EmpNation" autocomplete="off" class="valid"></select>
                </div>
                <label for="EmpBirthday" class="layui-form-label">
                    <span class="we-red">*</span>出生日期
                </label>
                <div class="layui-input-inline">
                    <input class="layui-input" name="EmpBirthday" autocomplete="off" id="EmpBirthday">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="EmpAddress" class="layui-form-label">
                    地址
                </label>
                <div class="layui-input-block">
                    <input type="text" id="EmpAddress" name="EmpAddress" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="EmpTel" class="layui-form-label">
                    固定电话
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="EmpTel" name="EmpTel" autocomplete="off" class="layui-input">
                </div>
                <label for="EmpPhone" class="layui-form-label">
                    <span class="we-red">*</span>手机
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="EmpPhone" name="EmpPhone" autocomplete="off" lay-verify="required|phone"  class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="DepID" class="layui-form-label">
                    <span class="we-red">*</span>部门编号
                </label>
                <div class="layui-input-inline">
                    <select id="DepID" name="DepID" autocomplete="off" class="valid"></select>
                </div>
                <label for="RoleID" class="layui-form-label">
                    <span class="we-red">*</span>角色编号
                </label>
                <div class="layui-input-inline">
                    <select id="RoleID" name="RoleID" autocomplete="off" class="valid"></select>
                </div>
            </div>
            <div class="layui-form-item" style="display:none;">
                <label for="EmpJWDID" class="layui-form-label">
                    <span class="we-red">*</span>机务段
                </label>
                <div class="layui-input-inline">
                    <select id="EmpJWDID" name="EmpJWDID" autocomplete="off" class="valid">
                        <option value="1">1</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="EmpDescription" class="layui-form-label">
                    备注信息
                </label>
                <div class="layui-input-block">
                    <textarea id="EmpDescription" name="EmpDescription" autocomplete="off" placeholder="请输入" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="add">确定</button>
                </div>
            </div>
        </form>
    </div>
    <script src="../../../lib/layui/layui.js" charset="utf-8"></script>
    <script src="../../../static/js/common.js" charset="utf-8"></script>

    <script>
        layui.use(['form', 'jquery', 'layer', 'laydate'], function () {
            var form = layui.form,
                $ = layui.jquery,
                layer = layui.layer,
                laydate = layui.laydate;
            //执行一个laydate实例
            laydate.render({
                elem: '#EmpBirthday',
                trigger: 'click'
            });
            //自定义验证规则
            form.verify({
                nikename: function (value) {
                    if (value.length < 2) {
                        return '姓名至少得2个字符啊';
                    }
                },
                pass: [/(.+){6,12}$/, '密码必须6到12位'],
                repass: function (value) {
                    if ($('#EmpPwd').val() != $('#ReEmpPwd').val()) {
                        return '两次密码不一致';
                    }
                }
            });
            $.support.cors = true;
            $(function () {
                //加载民族
                var nations = ["汉族", "蒙古族", "回族", "藏族", "维吾尔族", "苗族", "彝族", "壮族", "布依族", "朝鲜族", "满族", "侗族", "瑶族", "白族", "土家族",
                    "哈尼族", "哈萨克族", "傣族", "黎族", "傈僳族", "佤族", "畲族", "高山族", "拉祜族", "水族", "东乡族", "纳西族", "景颇族", "柯尔克孜族",
                    "土族", "达斡尔族", "仫佬族", "羌族", "布朗族", "撒拉族", "毛南族", "仡佬族", "锡伯族", "阿昌族", "普米族", "塔吉克族", "怒族", "乌孜别克族",
                    "俄罗斯族", "鄂温克族", "德昂族", "保安族", "裕固族", "京族", "塔塔尔族", "独龙族", "鄂伦春族", "赫哲族", "门巴族", "珞巴族", "基诺族"];
                var staff_nation = document.getElementById("EmpNation");
                for (var i in nations) {
                    staff_nation.add(new Option(nations[i], nations[i]));
                }
                form.render();
                //加载部门信息，角色信息，机务段信息
                var depajax = $.ajax({
                    url: ApiUrl + "Dep/Sel",
                    type: 'post',
                    headers: {
                        "userid": JSON.parse(window.localStorage.authInfo).UserId,
                        "signtoken": JSON.parse(window.localStorage.authInfo).SignToken,
                    },
                    success: function (data) {
                        if (data.StatusCode == 200) {
                            $(data.Data).each(function (index, item) {
                                $("#DepID").append("<option value='" + item.DepID + "'>" + item.DepName + "</option>");
                            });
                            form.render();//必须，不然动态加载项不显示
                        }
                        else if (data.StatusCode == 403) {
                            top.layer.alert(data.Info + ",请重新登录！", { closeBtn: 0 }, function () {
                                top.location.href = "http://" + top.location.host + "/login.html";
                            })
                        }
                        else {
                            layer.msg(data.Info);
                        }
                    }
                });
                var roleajax = $.ajax({
                    url: ApiUrl + "Role/Sel",
                    type: 'post',
                    headers: {
                        "userid": JSON.parse(window.localStorage.authInfo).UserId,
                        "signtoken": JSON.parse(window.localStorage.authInfo).SignToken,
                    },
                    success: function (data) {
                        if (data.StatusCode == 200) {
                            $(data.Data).each(function (index, item) {
                                $("#RoleID").append("<option value='" + item.RoleID + "'>" + item.RoleName + "</option>");
                            });
                            form.render();//必须，不然动态加载项不显示
                        }
                        else if (data.StatusCode == 403) {
                            top.layer.alert(data.Info + ",请重新登录！", { closeBtn: 0 }, function () {
                                top.location.href = "http://" + top.location.host + "/login.html";
                            })
                        }
                        else {
                            layer.msg(data.Info);
                        }
                    }
                });
                //加载部门信息，角色信息，机务段信息结束
                //加载人员信息
                var editdata = {};
                var empajax = $.ajax({
                    url: ApiUrl + "Employee",
                    type: 'get',
                    dataType: 'json',
                    cache: false,
                    headers: {
                        "userid": JSON.parse(window.localStorage.authInfo).UserId,
                        "signtoken": JSON.parse(window.localStorage.authInfo).SignToken,
                    },
                    data: { id: getUrlParam("ID") },
                    success: function (data) {
                        editdata = JSON.parse(data);
                    }
                });
                //加载人员信息结束
                //相关ajax加载完成后，绑定数据
                $.when(depajax, roleajax, empajax)
                    .done(function () {
                        console.log(editdata)
                        for (var key in editdata) {
                            if (key == "EmpSex") {
                                $(":radio[name='" + key + "'][value='" + $.trim(editdata[key]) + "']").prop("checked", "checked");
                            }
                            else if (key == "EmpPwd") {
                                $("#" + key).val($.trim(editdata[key]));
                                $("#ReEmpPwd").val($.trim(editdata[key]));
                            }
                            else if (key == "EmpBirthday") {
                                $("#" + key).val(editdata[key].substring(0, 10));
                            }
                            else {
                                $("#" + key).val($.trim(editdata[key]));
                            }
                        }
                        form.render();
                    })
                    .fail(function () {
                        //console.log("test4")
                    });
            })
            //监听提交
            form.on('submit(add)', function (formdata) {
                $.ajax({
                    url: ApiUrl + "Employee/Edit",
                    type: 'post',
                    dataType: 'json',
                    cache: false,
                    headers: {
                        "userid": JSON.parse(window.localStorage.authInfo).UserId,
                        "signtoken": JSON.parse(window.localStorage.authInfo).SignToken,
                    },
                    data: formdata.field,
                    success: function (data) {
                        console.log(data)
                        if (data.StatusCode == 200) {
                            layer.alert("编辑成功", { icon: 6 }, function (index) {
                                parent.layer.closeAll();
                            })
                        }
                        else if (data.StatusCode == 403) {
                            top.layer.alert(data.Info + ",请重新登录！", { closeBtn: 0 }, function () {
                                top.location.href = "http://" + top.location.host + "/login.html";
                            })
                        }
                        else {
                            layer.msg(data.Info);
                        }
                    }
                });
                return false;
            });
        });
    </script>
</body>
</html>